package com.dykj.dati.controller.h5;


import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dykj.dati.config.RedisCache;
import com.dykj.dati.model.t.*;
import com.dykj.dati.model.user.HUser;
import com.dykj.dati.service.t.ITJiaojuanService;
import com.dykj.dati.service.user.IHUserService;
import com.dykj.dati.utils.ReturnUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.dykj.dati.controller.h5.KSController.*;
import static com.dykj.dati.utils.ServiceUtil.hasUser;
import static com.dykj.dati.utils.ServiceUtil.isJswb;

/**
 * @Author: yc
 * @Date: 2021/9/22 15:48
 */
@CrossOrigin
@Controller
@RequestMapping("/dth5")
public class JiaoJuanController {

    public static Map<String,String> tkSuccessMap=new HashMap<>();
    public static int tiNum=20;
    @Autowired
    private ITJiaojuanService itJiaojuanService;
    @Autowired
    private IHUserService hUserService;
    @Autowired
    private RedisCache redisCache;
    @GetMapping("/jj2")
    @ResponseBody
    public ModelMap jj2( String tiJiaoJuans) {
        TiJiaoJuan tiJiaoJuan= JSON.toJavaObject(JSONObject.parseObject(tiJiaoJuans), TiJiaoJuan.class);
        //tiJiaoJuan=getti();
        if(! hasUser(tiJiaoJuan.getUid())){
            return ReturnUtil.Error("非法访问!");
        }
        if(! isJswb(tiJiaoJuan.getUid())||tiJiaoJuan.getType()==0){
            if(!redisCache.hasKey(tiJiaoJuan.getCid())){
                return ReturnUtil.Error("非法超时!！");
            }
            Date dtDate=(Date)redisCache.get(tiJiaoJuan.getCid());
            //Date dtDate=DateUtil.offset(DateUtil.date(), DateField.SECOND, RandomUtil.randomInt(-600,0));
            //下边这行需要干屌
            /*tiJiaoJuan=getti();*/
            //上边这行需要干屌
            TJiaojuan tJiaojuan=new TJiaojuan();


            tJiaojuan.setId(tiJiaoJuan.getCid());
            tJiaojuan.setCreateTime(DateUtil.date());
            tJiaojuan.setUid(tiJiaoJuan.getUid());
            tJiaojuan.setType(tiJiaoJuan.getType());


            long betweenDay = DateUtil.between(dtDate, DateUtil.date(), DateUnit.MS);
            betweenDay=betweenDay/1000>610?60000:betweenDay;
            tJiaojuan.setYsTime((int)betweenDay);
            String formatBetween = DateUtil.formatBetween(betweenDay, BetweenFormatter.Level.SECOND);
            tJiaojuan.setYs(formatBetween);
            tJiaojuan.setJjTime(DateUtil.date());

            List<TXiangQing> tXiangQingList=new ArrayList<>();
            List<UJiaoJuan> uJiaoJuanList=tiJiaoJuan.getUJiaoJuanList();
            uJiaoJuanList.removeAll(Collections.singleton(null));
            int fs=0;
            for(UJiaoJuan uJiaoJuan:uJiaoJuanList){
                TXiangQing tXiangQing=new TXiangQing();
                tXiangQing.setTitle(tizongMap.get(uJiaoJuan.getTid()));
                tXiangQing.setChoose(uJiaoJuan.getK());
                tXiangQing.setChooseWz(titleXuanxiang.get(uJiaoJuan.getTid()+"_"+uJiaoJuan.getK()));
                tXiangQing.setDuicuo(StringUtils.equals(uJiaoJuan.getK(),tkSuccessMap.get(uJiaoJuan.getTid()))?1:0);
                tXiangQing.setZq(tkSuccessMap.get(uJiaoJuan.getTid()));
                tXiangQing.setZqWz(titleXuanxiang.get(uJiaoJuan.getTid()+"_"+tkSuccessMap.get(uJiaoJuan.getTid())));
                tXiangQingList.add(tXiangQing);


                fs+= StringUtils.equals(uJiaoJuan.getK(),tkSuccessMap.get(uJiaoJuan.getTid()))?5:0;
            }
            tJiaojuan.setFs(fs);
            tJiaojuan.setJl(JSON.toJSONString(tXiangQingList));
            tJiaojuan.setTXiangQingList(tXiangQingList);

            HUser hUser=hUserService.getById(tiJiaoJuan.getUid());
            tJiaojuan.setUserName(hUser.getName());
            tJiaojuan.setPhone(hUser.getPhone());



           /* txiangxiMap.put(tiJiaoJuan.getCid(),tJiaojuan);*/


            itJiaojuanService.save(tJiaojuan);

//判断答题类型
           /* if(tiJiaoJuan.getType()==1){//竞赛模式
                redisCache.put("jswb:"+tiJiaoJuan.getUid(),0);
            }*/
            //答题后的List在这里添加
            //list.add(uid);
            return ReturnUtil.Success("成功");
        }else{
            return ReturnUtil.Error("该账号已经答过题！");
        }
    }


    public TiJiaoJuan  getti(){
        String uid="5ee7f08dbaed4ab48c6c141c4b54d96b";
        String cid=IdUtil.simpleUUID();
        TiJiaoJuan tiJiaoJuan=new TiJiaoJuan();
        tiJiaoJuan.setUid(uid);
        tiJiaoJuan.setCid(cid);
        tiJiaoJuan.setType(1);
        List<UJiaoJuan> uJiaoJuanList=new ArrayList<>();

        Map<Integer,String> xx=new HashMap<>();
        xx.put(0,"A");
        xx.put(1,"B");
        xx.put(2,"C");


        List<TiZong> chouTi=new ArrayList<>();
        while(chouTi.size()<tiNum){
            int i= RandomUtil.randomInt(0,tizongList.size()-1);
            if(!chouTi.contains(tizongList.get(i))){
                chouTi.add(tizongList.get(i));
            }
        }

        for(TiZong tiZong:chouTi){
            UJiaoJuan u=new UJiaoJuan();
            u.setTid(tiZong.getTid());
            u.setK(xx.get(RandomUtil.randomInt(0,2)));
            uJiaoJuanList.add(u);
        }
        tiJiaoJuan.setUJiaoJuanList(uJiaoJuanList);
        Date d=DateUtil.offset(DateUtil.date(), DateField.SECOND, RandomUtil.randomInt(-600,0));
        redisCache.put(cid,d);
        redisCache.expire(cid,610);
        return   tiJiaoJuan;
    }
}
